
package com.tools.cleanmaster.ui.card.factory;

import android.text.TextUtils;

import com.hm.ads.sdk.HmAdsSDK;
import com.hm.adsbuisiness.bean.HmAdUnionPlace;
import com.tools.ad.ADModule;
import com.tools.ad.EntranceType;
import com.tools.ad.adscene.AdScene;
import com.tools.ad.strategy.ADStrategyManager;
import com.tools.ad.strategy.IStrategyExecutor;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.ui.card.ACardItem;
import com.tools.cleanmaster.ui.card.CardType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 卡片数据辅助类,用于在不同的场景中，加载广告、获取卡片数据（FunctionCardFactory提供）
 */
public class CardDataHelper {
    public static final String TAG = "CardDataHelper";
    /**
     * 当前入口类型
     */
    private EntranceType mEntranceType;

    public CardDataHelper(EntranceType entrance) {
        mEntranceType = entrance;
    }

    /**
     * 扫描卡片信息，进行展示
     */
    public List<ACardItem> produceCards() {
        FunctionCardFactory functionCardFactory = new FunctionCardFactory();
        List<ACardItem> functionList = functionCardFactory.produceCards(mEntranceType);

        insertRecommendFunctionCardItem(functionList);

        List<ACardItem> showItems = new ArrayList<>();
        if (functionList != null && !functionList.isEmpty()) {
            showItems.addAll(functionList);
        }

        Collections.sort(showItems);
        for (int i = showItems.size() - 1; i >= 0; i--) {
            ACardItem item = showItems.get(i);
            item.setPosition(i);
        }

        return showItems;
    }

    /**
     * 插入推荐的功能卡片（优先级BOOST>POWER>COOL）
     *
     * @param functionList
     */
    private void insertRecommendFunctionCardItem(List<ACardItem> functionList) {
        if (functionList == null || functionList.isEmpty()) {
            return;
        }

        EntranceType[] filterType = null;

        if (mEntranceType == EntranceType.HOME_BOOST_COMPLETED) {
            filterType = new EntranceType[]{
                    EntranceType.FUNCTION_POWER,
                    EntranceType.FUNCTION_COOL
            };
        }
        if (mEntranceType == EntranceType.HOME_POWER_COMPLETED) {
            filterType = new EntranceType[]{
                    EntranceType.FUNCTION_BOOST,
                    EntranceType.FUNCTION_COOL
            };
        }
        if (mEntranceType == EntranceType.HOME_CPU_COMPLETED) {
            filterType = new EntranceType[]{
                    EntranceType.FUNCTION_BOOST,
                    EntranceType.FUNCTION_POWER
            };
        }

        if (mEntranceType == EntranceType.HOME_TRASH_COMPLETED) {
            filterType = new EntranceType[]{
                    EntranceType.FUNCTION_BOOST,
                    EntranceType.FUNCTION_POWER,
                    EntranceType.FUNCTION_COOL
            };
        }

        if (filterType == null) {
            return;
        }

        EntranceType entranceType = filterEntranceType(filterType);
        if (entranceType == null) {
            return;
        }

        String name = null;
        if (entranceType == EntranceType.FUNCTION_BOOST) {
            name = CardType.BOOSTER.key;
        } else if (entranceType == EntranceType.FUNCTION_COOL) {
            name = CardType.COOLER.key;
        } else if (entranceType == EntranceType.FUNCTION_POWER) {
            name = CardType.POWER.key;
        }

        if (name != null && !TextUtils.isEmpty(name)) {
            ACardItem item = ACardItem.createCardItem(name, 1, entranceType);
            functionList.add(item);
        }
    }

    private EntranceType filterEntranceType(EntranceType... entranceTypes) {
        CleanApplication imContext = CleanApplication.getInstance();

        EntranceType returnType = null;
        for (EntranceType entranceType : entranceTypes) {
            HmAdUnionPlace adUnionPlace = HmAdsSDK.getInstance().getUnionAd(entranceType.getPid());
            if (adUnionPlace == null) {
                continue;
            }

            ADModule adModule = imContext.getADModule();
            ADStrategyManager strategyManager = adModule.getADStrategyManager();
            IStrategyExecutor functionExecutor = strategyManager.getExecutorByEntranceType(entranceType);
            if (functionExecutor.check(adUnionPlace)) {
                returnType = entranceType;
                break;
            }
        }
        return returnType;
    }

    /**
     * 根据类型，请求对应场景的广告
     *
     * @param entranceType
     */
    public static void requestAd(EntranceType entranceType) {
        ADModule adModule = CleanApplication.getInstance().getADModule();
        if (adModule == null) {
            //AdModule 可能初始化失败导致为空
            return;
        }

        AdScene advertScene = adModule.getAdSceneManager().getAdScene(entranceType);
        if (advertScene != null) {
            advertScene.load(false);
        }
    }


}
